package projet.entity;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name="location")
@NamedQueries({
    @NamedQuery(name="Location.findAll", query="SELECT l FROM Location l ORDER BY l.city"),
    @NamedQuery(name="Location.findById", query="SELECT l FROM Location l WHERE l.id = :id"),
    @NamedQuery(name="Location.findAllByCity", query="SELECT l FROM Location l WHERE l.city like :city ORDER BY l.city"),
    @NamedQuery(name="Location.findSessions", query="SELECT s FROM Session s WHERE s.location.id = :id")})
public class Location {
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	private Long id;
	
	@Column(nullable=false, unique=true)
	private String city;

	@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "location")
	private List<Session> sessions;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public void setSessions(List<Session> sessions) {
		this.sessions = sessions;
	}

	public List<Session> getSessions() {
		return sessions;
	}
	
	public void addSession(Session s) {
		if (sessions == null) {
			sessions = new ArrayList<Session>();
		}
		s.setLocation(this);
		sessions.add(s);
	}

}
